// JavaScript Document
/*************************/
/*程序作者：何武昌
/*创建时间：2013-08-02
/*程序说明：
/*该程序用来监控页面上可输入表单的状态，如果修改了，会动态保存到数据
/*因页面load时可以不需要该功能，所以采用惰性加载。
/*程序是基于MVVM和订阅者数据模式实现。
/*************************/
/*define(
	function(require){*/
	observer = function(){
		return new observer;//jquery式 无new实力化	
	}
	observer.readyPublish = [];
	//订阅
	observer.addSubscriber = function(callback) {
		this.suberscribers = [],
		this.suberscribers[this.suberscribers.length] = callback;
	}
	//退订
	observer.removeSuberscriber = function(callback) {
		for(var i = 0, suber; suber = this.subscribers[i++];) {
			if(suber == callback) {
				delete suber;	
			}	
		}	
	}
	//发布
	observer.publish = function() {
		for(var i = 0, puber; puber = this.readyPublish[i++];) {
			this.suberscribers[0](mes);
			puber.tag.suberscribers[0](data);	
		}
	}
	//将对象o具有观测者功能
	observer.make = function(o) {
		for(var i in this) {
			o[i] = this[i];
			o.subscribers = [];	
		}
	}
	//获取发布者并绑定事件
	//**********************
	/*changeType 3个状态”
	/*为空表示表单所在的数据项 是第一次出现，sameType表示相同数据项已经出现，changed表示该表单在一次保存中出现过多次。
	//**************************/
	observer.setPuber = function(tags) {
		for(var i = 0, tag; tag = tags[i++];) {
			if(tag.getAttribute("type") != "checkbox") {
				tag.onchange = function() {
					observer.monitoring(this);
				}
			}
		}
	}
	observer.monitoring = function(that) {
		var data = {}, name, m, k, tempType, trRow;
		var reg, changeType, reader, parentRow, val;
		changeType = "default";
		name = that.name.split("_");
		switch(name[0]) {
			case "miao":
			case "yewu":
			case "flw":
				tempType = name[0];
				break;
			case "step":
				parentRow = that.parentNode.parentNode;
				trRow = parentRow.rowIndex;
				tempType = "step" + "_" + trRow;
				break;
			case "quan":
				parentRow = that.parentNode.parentNode;
				trRow = parentRow.parentNode.rows.length - parentRow.rowIndex;
				tempType = "quan" + "_" + trRow;
			default:
				break;	
		}		
		data.changeName = name[1];
		if("flw_check" == that.className) {
			if(that.checked) {
				data.changeVal = 1;
			}	
			else {
				data.changeVal = 0;	
			}
		} else {
			data.changeVal = that.value.Trim();
		}
		for(var j in observer.readyPublish) {
			reader = observer.readyPublish[j];
			if(tempType == reader.type && "update" == reader.data.type) {
				if(0 <= reader.data.change.indexOf(name[1])) {
					changeType = "changed";	
					m = j;
					for(var n = 0, named; named = reader.data.tags[n++];) {
						if(that.name == named.name) {
							k = n - 1;
							break;
						}	
					}
					break;
				}
				else {
					changeType = "sameType";
					m = j;	
				}	
			}
			else if("insert" == reader.data.type && tempType == reader.type){
				var tempArr = reader.data.change.split(",");
				tempArr[that.parentNode.cellIndex] = "\"" + data.changeVal + "\"";
				reader.data.change = tempArr.join(",");
			}
		}
		switch(changeType) {
			case "changed":	
				reg = new RegExp(name[1]+'=\"(.*?)\"');
				if(data.changeVal == dba.monitoring.checkVal) {
					observer.readyPublish[m].data.change = observer.readyPublish[m].data.change.replace(reg, "");
					observer.readyPublish[m].data.tags.splice(k, 1);
					//如果是最后一个，就把该条数据删除
					if(0 == observer.readyPublish[m].data.tags.length) {
						observer.readyPublish.splice(m, 1);	
					}
				}
				else {
					observer.readyPublish[m].data.change = observer.readyPublish[m].data.change.replace(reg, name[1]+"=\""+data.changeVal+"\"");
				}
				break;
			case "sameType":
				observer.readyPublish[m].data.change += ","+	name[1]+"=\""+data.changeVal +"\"";
				var len = observer.readyPublish.length;
				observer.readyPublish[m].data.tags[observer.readyPublish[m].data.tags.length] = that;
				break;
			case "default":
				//对于日历插件这种改变input的值，但是最终的值又和默认值一样的情况(会触发onchange事件)，不需要保存到变动数组(readyPublish)里面
				if("time" == that.getAttribute("field")) {
					if(that.value == dba.monitoring.checkVal) {
						return false;	
					}
				}
				//构建每个更新的sql语句
				switch(name[0]) {
					case "flw":
						data.head = "UPDATE BAS_WMFSTEPOBJ SET";
						data.where = "STPID" + "=\"" + dba.$("stpid").value + "\"";
						break;
					//对于步骤流转信息和权限多行信息，用行号来确定。
					case "step":
						data.head = "UPDATE BAS_WMFSTEPRLT SET";
						data.where = "STPCHID=\"" + parentRow.cells[6].getElementsByTagName("input")[0].value + "\",OPCON=\"" + parentRow.cells[6].getElementsByTagName("input")[0].value + "\"";
						break;
					case "quan":
						data.head = "UPDATE BAS_PRVOBJSLV SET";
						data.where = "PRVTYPE=\"" + parentRow.cells[3].getElementsByTagName("SELECT")[0].value + "\",GUPNUM=\"" + parentRow.cells[4].getElementsByTagName("input")[0].value + "\"HDSQLCON=\"" + parentRow.cells[5].getElementsByTagName("input")[0].value + "\"";
						break;
					case "yewu":
						data.head = "UPDATE BAS_BUSSOBJ SET";
						data.where = "DOCID=\"" + dba.$("yewuid").value +"\"";
						break;
					case "miao":
						data.head = "UPDATE BAS_PRVOBJ SET";
						data.where = "OID=\"" + dba.$("OID") +"\"";
					default:
						break;	
				}			
				data.type = "update";	
				data.tags = [];
				data.tags[0] = that;
				data.change = data.changeName + "=\"" + data.changeVal + "\"";
				observer.readyPublish[observer.readyPublish.length] = {
					type : tempType,
					data : data	
				};
				break;
			default:
				break;
		}
	}
	observer.insert = function(typea, num, where) {
		var data = {}, tempType;
		data.type = "insert";
		if(typea == "flw") {
			tempType = "step" + "_" + num;
			data.head = "INSERT INTO BAS_WMFSTEPRLT (BEGDA,ENDDA,RLTST,RLLTP,RLTGP,COHRID,STPCHID,DYRLT,RLTCOL,OPCON,DLFLG,ASNM,FLWID,STPID) VALUES";
			data.change = where;	
		}
		else {
			tempType = "quan" + "_" + num;	
			data.head = "INSERT INTO BAS_PRVOBJSLV (BEGDA,ENDDA,OHRID,PRVTYPE,GUPNUM,HDSQLCON,COMMENT,DLFLG,ASNM,OID,HDOHRID,SQNM) VALUES";
			data.change = where;
		}	
		data.tags = []
		observer.readyPublish[observer.readyPublish.length] = {
			type : tempType,
			data : data	
		};
	}
	//用于检查删除行时的处理
	observer.checkDel = function(tag, row) {
		var data
		for(var i = 0, reader; reader = observer.readyPublish[i++];) {
			if("step_"+row == reader.type) {
				if("insert" == reader.data.type) {
					observer.readyPublish.splice(i-1, 1);//如果是新增加的行
					if(0 == observer.readyPublish.length) {
						dba.saveBut.style.display = "none";		
					}
				}
				else {
					reader.data.where += ',DLFLG="0"';	
				}
				return false;//如果满足这两种情况就不需要继续了。
			}	
		}
		//执行到这来说明前面的都不满足，给readerPublish新X如一个数据。	
		data.type = "update";
		data.change = 'DLFLG="0"';
		data.head = "UPDATE BAS_WMFSTEPRLT SET";
		data.tags = [];
		data.where = "STPCHID=\"" + tag.cells[6].getElementsByTagName("input")[0].value + "\",OPCON=\"" + tag.cells[6].getElementsByTagName("input")[0].value + "\"";
		observer.readyPublish[observer.readyPublish.length] = {
			type : tempType,
			data : data	
		};
		dba.saveBut.style.display = "block";	
	}
	//return observer;
/*	}
);*/